#include "mainwindow.h"
#include "ui_mainwindow.h"

//连接|断开
void MainWindow::on_btn_link_clicked()
{
    if(LangureStu == 1)
    {
        LangureStr = "Connect";
    }
    else
    {
        LangureStr = "连  接";
    }
    if(ui->btn_link->text() == LangureStr || ui->btn_link_bat->text() == LangureStr)
    {
        //下拉菜单控件失能
        ui->cbx_port->setEnabled(false);
		ui->cbx_port_bat_f210->setEnabled(false);
		LangureStr_Init_Btn_Unlink(ui->btn_link);
		LangureStr_Init_Btn_Unlink(ui->btn_link_bat);

        LangureStr_Init_Testing(ui->te_prin);
        LangureStr_Init_Testing(ui->te_prin_bat);
        //设置串口名
		if(ui->btn_left_bat_f210->isChecked())
		{
			CPortName = ui->cbx_port_bat_f210->currentText();
		}
		else
		{
			CPortName = ui->cbx_port->currentText();
		}
        if(!CPortName.isEmpty())
        {
            QString PortName = CPortName.split(":").at(0);
            PortSerial->setPortName(PortName);

            RecvStu = false;
            ConnectStu = 1;

            //设置波特率
            PortSerial->setBaudRate(115200);
            //设置停止位
            PortSerial->setStopBits(QSerialPort::OneStop);
            //设置数据位数
            PortSerial->setDataBits(QSerialPort::Data8);
            //设置奇偶校验
            PortSerial->setParity(QSerialPort::NoParity);
            //设置流控制
            PortSerial->setFlowControl(QSerialPort::NoFlowControl);
			
            //打开串口
            if(!PortSerial->open(QIODevice::ReadWrite))
            {
                if(LangureStu == 1)
                {
                    LangureStr = "Unable to open serial port";
                }
                else
                {
                    LangureStr = "无法打开串口";
                }
                InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
                InsertText(ui->te_prin_bat, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);

                on_btn_link_clicked();
                return;
            }

            PortStu = PORT_STU_SYNC;
            SyncTimer->start(1000);
        }
        else
        {
            if(ConnectStu)
            {
                on_btn_link_clicked();
            }
            else
            {
                //
                ui->cbx_upg->setCheckState(Qt::Unchecked);
                ui->cbx_write->setCheckState(Qt::Unchecked);
                ui->cbx_mtsn_write->setCheckState(Qt::Unchecked);

                ui->cbx_port->setEnabled(true);
                ui->cbx_port_bat_f210->setEnabled(true);
                LangureStr_Init_Btn_Link(ui->btn_link);
                LangureStr_Init_Btn_Link(ui->btn_link_bat);

                // 20230211-1617 新增检测错误提示
                if(LangureStu == 1)
                {
                    LangureStr = "Detection failed, no ports can be connected!";
                }
                else
                {
                    LangureStr = "检测失败，没有可以连接的端口!";
                }
                InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
                InsertText(ui->te_prin_bat, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
            }
            return;
        }
    }
    else
    {
        QByteArray LineStr;
        LineStr.clear();
        pc_to_mcu_send_data(LineStr, SEND_PORT_DISC);

        ConnectStu = 0;
        DevVersion = 0;

        if(SyncTimer->isActive())
            SyncTimer->stop();
        if(WaitTimer->isActive())
            WaitTimer->stop();
        //关闭串口
        PortSerial->clear();
        PortSerial->close();

        PortStu = PORT_STU_NULL;
        LastPortStu = PORT_STU_NULL;

        ui->progressBar->setValue(0);

        Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);
        //
        ui->cbx_upg->setCheckState(Qt::Unchecked);
        ui->cbx_write->setCheckState(Qt::Unchecked);
        ui->cbx_mtsn_write->setCheckState(Qt::Unchecked);

        if(LangureStu == 1)
        {
            LangureStr = "Current equipment version number: unrecognized";
        }
        else
        {
            LangureStr = "当前设备版本号：未识别";
        }
        ui->lbl_dev_ver->setText(LangureStr);
        ui->cbx_port->setEnabled(true);
        ui->cbx_port_bat_f210->setEnabled(true);
		LangureStr_Init_Btn_Link(ui->btn_link);
        LangureStr_Init_Btn_Link(ui->btn_link_bat);
    }
}
// 电池修复-连接按钮
void MainWindow::on_btn_link_bat_clicked()
{
    on_btn_link_clicked();
}

//读取盖板码
void MainWindow::on_btn_mtsn_read_clicked()
{
    if(!Check_cbx_model_read_Must_Select(ui->cbx_model_read, ui->te_prin))
    {
        return;
    }
    SyncTimer->stop();
    Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);

    //发送读取盖板码请求
    QByteArray LineStr;
    LineStr.clear();
    LastPortStu = PORT_STU_READ_MTSN;
    PortStu = PORT_STU_WAIT;
#if 1
    Wait_TimeOut_to_Send_Three_Time(LineStr, PORT_STU_READ_MTSN, SEND_READ_MTSN_REQ);
#else
    pc_to_mcu_send_data(LineStr, SEND_READ_MTSN_REQ);
    Wait_TimeOut_to_Mcu_Answer(PORT_STU_WAIT);
#endif
    if(PORT_STU_READ_MTSN != PortStu)
    {
        if(PORT_STU_BUSY == PortStu)
        {
            if(LangureStu == 1)
            {
                LangureStr = "The device is busy, please operate later!";
            }
            else
            {
                LangureStr = "设备繁忙，请稍后操作!";
            }
            InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        else if(PORT_STU_TIMEOUT == PortStu)
        {
            if(LangureStu == 1)
            {
                LangureStr = "The device timed out. Please check the device connection status and try again!";
            }
            else
            {
                LangureStr = "设备超时，请检查设备连接状态后重试!";
            }
            InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        else
        {
            if(LangureStu == 1)
            {
                LangureStr = "Failed to read MtSN!";
            }
            else
            {
                LangureStr = "读取盖板码失败!";
            }
            InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
        Port_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);

        SyncTimer->start(TIMER_SYNC_TIME_OUT);
        return;
    }

    if(!RecvMtSN.isEmpty())
    {
        if((RecvMtSN.size() == 44 || RecvMtSN.size() == 88))
        {
			if(LangureStu == 1)
			{
				LangureStr = tr("The MtSN reading is completed");
			}
			else
			{
				LangureStr = QString("盖板码读取完成");
			}
            InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
            RecvMtSN.replace('\0',' ');
            if(ModelStu < Model_12)
            {
                if(LangureStu == 1)
                {
                    LangureStr = tr("MtSN: ") + RecvMtSN;
                }
                else
                {
                    LangureStr = QString("盖板码: ") + RecvMtSN;
                }
                InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
            }
            else
            {
                if(LangureStu == 1)
                {
                    LangureStr = tr("Drive IC MtSN: ") + RecvMtSN.mid(0,44);
                }
                else
                {
                    LangureStr = QString("驱动IC盖板码: ") + RecvMtSN.mid(0,44);
                }
                InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
                if(LangureStu == 1)
                {
                    LangureStr = tr("TP-IC MtSN: ") + RecvMtSN.mid(44,44);
                }
                else
                {
                    LangureStr = QString("TP-IC 盖板码: ") + RecvMtSN.mid(44,44);
                }
                InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
            }
        }
        else
        {
            if(LangureStu == 1)
            {
                LangureStr = "MtSN reading failed!";
            }
            else
            {
                LangureStr = "盖板码读取失败!";
            }
            InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        }
    }
    else
    {
        if(LangureStu == 1)
        {
            LangureStr = "MtSN reading failed!";
        }
        else
        {
            LangureStr = "盖板码读取失败!";
        }
        InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
    }

    Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    SyncTimer->start(TIMER_SYNC_TIME_OUT);

    if(PORT_STU_NULL == PortStu)
    {
		LangureStr_Init_Btn_Unlink(ui->btn_link);
        LangureStr_Init_Btn_Unlink(ui->btn_link_bat);
        on_btn_link_clicked();
    }
}
//写入盖板码
void MainWindow::on_btn_mtsn_write_clicked()
{
    if(!Check_cbx_model_read_Must_Select(ui->cbx_model_read, ui->te_prin))
    {
        return;
    }
    SyncTimer->stop();
    Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);

    int SendCmd;
    if(ui->cbx_dev->isChecked())
    {
        SendCmd = SEND_BURN_MTSN_DEV;
    }
    else
    {
        SendCmd = SEND_BURN_MTSN_REQ;
    }

    if(Mtsn_Write_to_Mcu(SendCmd))
    {
        LangureStr_Init_BurnCode_Succeed(ui->te_prin);
        Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    }
    else
    {
        Port_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
    }

    SyncTimer->start(TIMER_SYNC_TIME_OUT);

    if(PORT_STU_NULL == PortStu)
    {
		LangureStr_Init_Btn_Unlink(ui->btn_link);
        LangureStr_Init_Btn_Unlink(ui->btn_link_bat);
        on_btn_link_clicked();
    }
}
//读取数据
void MainWindow::on_btn_read_clicked()
{
    if(ui->btn_left_disp_f210->isChecked())
    {
        //停止同步定时器
        SyncTimer->stop();
        Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);
        //文件过滤器
        QString filter;
        if(LangureStu == 1)
        {
            filter="all file(*.*)";
        }
        else
        {
            filter="所有文件(*.*)";
        }

        QString CfgDirName = CfgFile->value("OpenDir/Dir").toString();
        if(CfgDirName.isEmpty())
        {
            CfgDirName = FilePath;
        }
        //对话框标题
        QString dlgTitle;
        if(LangureStu == 1)
        {
            dlgTitle="Select a file";
        }
        else
        {
            dlgTitle="选择一个文件";
        }
        ReadFileName = QFileDialog::getOpenFileName(this,dlgTitle,CfgDirName,filter);
        //提示
        LangureStr_Init_ReadingData(ui->te_prin);
        //
        if(Read_Disp_Data_Form_Mcu(ReadFileName))
        {
            ui->progressBar->setValue(100);
            LangureStr_Init_ReadData_Succeed(ui->te_prin);
            Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
        }
        else
        {
            Port_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
        }

        SyncTimer->start(TIMER_SYNC_TIME_OUT);
        if(PORT_STU_NULL == PortStu)
        {
            LangureStr_Init_Btn_Unlink(ui->btn_link);
			LangureStr_Init_Btn_Unlink(ui->btn_link_bat);
            on_btn_link_clicked();
        }
    }
    else
    {
        if(!Check_cbx_model_read_Must_Select(ui->cbx_model_read, ui->te_prin))
        {
            return;
        }
        //停止同步定时器
        SyncTimer->stop();
        Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);
        //提示
        LangureStr_Init_ReadingData(ui->te_prin);
        if(Read_Data_Form_Mcu())
        {
            ui->progressBar->setValue(100);
            LangureStr_Init_ReadData_Succeed(ui->te_prin);
            Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
        }
        else
        {
            Port_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
        }

        SyncTimer->start(TIMER_SYNC_TIME_OUT);
        if(PORT_STU_NULL == PortStu)
        {
            LangureStr_Init_Btn_Unlink(ui->btn_link);
			LangureStr_Init_Btn_Unlink(ui->btn_link_bat);
            on_btn_link_clicked();
        }
    }
}
//写入数据
void MainWindow::on_btn_write_clicked()
{
    if(!Check_cbx_model_read_Must_Select(ui->cbx_model_read, ui->te_prin))
    {
        return;
    }
    SyncTimer->stop();
    Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);

    LangureStr_Init_BurningData(ui->te_prin);
    if(Burn_Data_to_Mcu())
    {
        ui->progressBar->setValue(100);
		LangureStr_Init_BurnData_Succeed(ui->te_prin);
        Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    }
    else
    {
        Port_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);

        ReadFileName = ui->te_file_write->toPlainText();
        QString BakReadFileName = ReadFileName + ".bak";

        QFileInfo BakReadFile(BakReadFileName);
        if(BakReadFile.isFile())
        {
            QFile::remove(BakReadFileName);
        }
    }

    SyncTimer->start(TIMER_SYNC_TIME_OUT);

    if(PORT_STU_NULL == PortStu)
    {
		LangureStr_Init_Btn_Unlink(ui->btn_link);
        LangureStr_Init_Btn_Unlink(ui->btn_link_bat);
        on_btn_link_clicked();
    }
}

//升级
void MainWindow::on_btn_upg_clicked()
{
    if(0 == UpgStu)
    {
        if(LangureStu == 1)
        {
            LangureStr = "The software version is low, please upgrade Light up assistant";
        }
        else
        {
            LangureStr = "软件版本低，请升级点亮助手";
        }
		InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        return;
    }

    SyncTimer->stop();
    Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);

    //...
    if(LangureStu == 1)
    {
        LangureStr = "The upgrade data is being transferred. Please do not disconnect the device...";
    }
    else
    {
        LangureStr = "正在传输升级数据，请勿断开设备...";
    }
	InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
    if(Upgrade_File_to_Mcu())
    {
        Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    }
    else
    {
        Port_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
    }

    SyncTimer->start(TIMER_SYNC_TIME_OUT);

    if(PORT_STU_NULL == PortStu)
    {
		LangureStr_Init_Btn_Unlink(ui->btn_link);
        LangureStr_Init_Btn_Unlink(ui->btn_link_bat);
        on_btn_link_clicked();
    }
}

bool MainWindow::Upgrade_File_to_Mcu()
{
    //升级提示类型
    // 0.无升级文件
    // 1.仅软件升级
    // 2.仅固件升级
    // 3.软件和固件升级
    int UpgTipType = 0;
    //升级文件
    QString VerFileName = FilePath + "/" + VersFileName;
    QFile VerFile(VerFileName);
#ifdef __UPRG_FPGA
    QString DevFileName = FilePath + "/" + FpgaFileName;
    QFile DevFile(DevFileName);
#endif

    switch(UpgStu)
    {
    case 1:
    case 2:
    {
        ui->progressBar->setValue(0);
        char LineBuff[512];
        qint64 Length;
        int VerSize = VerFile.size();
        int DevSize;
        int ToalFileSize = 0;
#ifdef __UPRG_FPGA
        if(2 == UpgStu)
        {
            DevSize = DevFile.size();
            ToalFileSize = VerSize + DevSize;
        }
        else
        {
            ToalFileSize = VerSize;
        }
#endif
        if(0 >= ToalFileSize)
        {
            if(LangureStu == 1)
            {
                LangureStr = "No upgrade file";
            }
            else
            {
                LangureStr = "无升级文件";
            }
            InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
        }
        int FileSize = 0;
        bool DoneStu = true;
        PageCnt = 0;

        QByteArray LineStr;
        LineStr.clear();
        AllFileSize = ToalFileSize;

        pc_to_mcu_send_data(LineStr,SEND_FILE_SIZE);
        Wait_Time_to_Wait_Mcu(TIME_MCU_TIME_OUT);

        DoneStu = true;
        if(VerSize == 0)
        {
            DoneStu = false;
        }
        else if(!VerFile.open(QIODevice::ReadOnly))
        {
            VerFile.close();
        }
        else if(PORT_STU_NULL != PortStu)
        {
            UpgTipType += 1;

            while(!VerFile.atEnd()){
                if(VerSize - FileSize > 512)
                {
                    Length = VerFile.read(LineBuff, 512);
                }
                else
                {
                    Length = VerSize - FileSize;
                    Length = VerFile.read(LineBuff, Length);
                }
                QByteArray VersBuff;
                VersBuff.clear();
                for(int i=0;i<Length;i++)
                {
                    VersBuff.append(LineBuff[i]);
                }
                if(0 != Length)
                {
                    Wait_TimeOut_to_Send_Three_Time(VersBuff, PORT_STU_UPG, SEND_DATA_UPG);
                    if(PORT_STU_UPG != PortStu)
                    {
                        ui->progressBar->setValue(0);
                        if(PORT_STU_BUSY == PortStu)
                        {
                            if(LangureStu == 1)
                            {
                                LangureStr = "The device is busy, please operate later!";
                            }
                            else
                            {
                                LangureStr = "设备繁忙，请稍后操作!";
                            }
							InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
                        }
                        else if(PORT_STU_TIMEOUT == PortStu)
                        {
                            if(LangureStu == 1)
                            {
                                LangureStr = "The device timed out. Please check the device connection status and try again!";
                            }
                            else
                            {
                                LangureStr = "设备超时，请检查设备连接状态后重试!";
                            }
							InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
                        }
                        else
                        {
                            if(LangureStu == 1)
                            {
                                LangureStr = "Upgrade failed!";
                            }
                            else
                            {
                                LangureStr = "升级失败!";
                            }
                            InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_FAILED);
                        }
                        FileSize = VerSize;
                        DoneStu = false;
#ifdef __UPRG_FPGA
                        if(1 == UpgStu)
                            ui->progressBar->setValue(0);
#endif
                        UpgTipType -= 1;
                        VerFile.close();
                        return false;
                    }
                }
                FileSize += Length;
                ui->progressBar->setValue((int)(99*FileSize/ToalFileSize));
            }
            VerFile.close();

            if(DoneStu == true)
            {
                QByteArray DoneBuff;
                DoneBuff.clear();
                Wait_TimeOut_to_Send_Three_Time(DoneBuff, PORT_STU_UPG, SEND_DATA_DONE);
                if(PORT_STU_ERROR == PortStu)
                {
                    return false;
                }
            }
        }
        if(PORT_STU_NULL == PortStu)
        {
            return false;
        }

#ifdef __UPRG_FPGA
        if(2 == UpgStu)
        {
            DoneStu = true;
            FileSize = 0;
            PageCnt = 0;
            if(!DevFile.open(QIODevice::ReadOnly))
            {
                DevFile.close();
            }
            else if(PORT_STU_NULL != PortStu)
            {
                UpgTipType += 2;
                while(!DevFile.atEnd()){
                    if(DevSize - FileSize > 512)
                    {
                        Length = DevFile.read(LineBuff, 512);
                    }
                    else
                    {
                        Length = DevSize - FileSize;
                        Length = DevFile.read(LineBuff, Length);
                    }
                    QByteArray DevBuff;
                    DevBuff.clear();
                    for(int i=0;i<Length;i++)
                    {
                        DevBuff.append(LineBuff[i]);
                    }
                    if(0 != Length)
                    {
                        pc_to_mcu_send_data(DevBuff, SEND_FPGA_UPG);

                        LastPortStu = PORT_STU_UPG;
                        PortStu = PORT_STU_WAIT;
                        WaitTimer->start(TIMER_SYNC_TIME_OUT);
                        while(PORT_STU_WAIT == PortStu)QApplication::processEvents();
                        WaitTimer->stop();
                        if(PORT_STU_UPG != PortStu)
                        {
                            ui->progressBar->setValue(0);
                            if(PORT_STU_BUSY == PortStu)
                            {
                                if(LangureStu == 1)
                                {
                                    LangureStr = "The device is busy, please operate later!";
                                }
                                else
                                {
                                    LangureStr = "设备繁忙，请稍后操作!";
                                }
								InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
                            }
                            else if(PORT_STU_TIMEOUT == PortStu)
                            {
                                if(LangureStu == 1)
                                {
                                    LangureStr = "The device timed out. Please check the device connection status and try again!";
                                }
                                else
                                {
                                    LangureStr = "设备超时，请检查设备连接状态后重试!";
                                }
								InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
                            }
                            else
                            {
                                if(LangureStu == 1)
                                {
                                    LangureStr = "Upgrade failed!";
                                }
                                else
                                {
                                    LangureStr = "升级失败!";
                                }
                                InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_FAILED);
                            }
                            FileSize = ToalFileSize;
                            DoneStu = false;
                            UpgTipType -= 2;
                            VerFile.close();
                            return false;
                        }
                    }
                    FileSize += Length;
                    ui->progressBar->setValue((int)(99*(FileSize+VerSize)/ToalFileSize));
                }
                DevFile.close();

                if(DoneStu == true)
                {
                    QByteArray DoneBuff;
                    DoneBuff.clear();
                    Wait_TimeOut_to_Send_Three_Time(DoneBuff, PORT_STU_UPG, SEND_FPGA_DONE);
                    if(PORT_STU_ERROR == PortStu)
                    {
                        return false;
                    }
                }
            }
            else
            {
                if(LangureStu == 1)
                {
                    LangureStr = "Disconnected";
                }
                else
                {
                    LangureStr = "已断开";
                }
				InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
                return false;
            }
        }
#endif

        if(PORT_STU_NULL != PortStu)
        {
            ui->progressBar->setValue(100);

            switch(UpgTipType) {
            //仅设备升级
            case 1:
            {
                if(LangureStu == 1)
                {
                    LangureStr = "Software data transmission is completed\n"
                                 "\tPlease disconnect the device and reconnect for device upgrade\n"
                                 "\tWait for the equipment upgrade to complete";
                }
                else
                {
                    LangureStr = "软件数据传输完成\n"
                                 "\t请断开设备，并重新连接进行设备升级\n"
                                 "\t等待设备升级完成";
                }
				InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_SUCCESS);
                break;
            }
            //仅固件升级
            case 2:
            {
                if(LangureStu == 1)
                {
                    LangureStr = "Firmware data upgrade completed\n"
                                 "\tPlease disconnect the device and reconnect for device upgrade\n"
                                 "\tWait for the equipment upgrade to complete";
                }
                else
                {
                    LangureStr = "固件数据升级完成\n"
                                 "\t请断开设备，并重新连接进行设备升级\n"
                                 "\t等待设备升级完成";
                }
				InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_SUCCESS);
                break;
            }
            //设备和固件升级
            case 3:
            {
                if(LangureStu == 1)
                {
                    LangureStr = "Upgrade data transmission completed\n"
                                 "\tPlease disconnect the device and reconnect for device upgrade\n"
                                 "\tWait for the equipment upgrade to complete";
                }
                else
                {
                    LangureStr = "升级数据传输完成\n"
                                 "\t请断开设备，并重新连接进行设备升级\n"
                                 "\t等待设备升级完成";
                }
				InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_SUCCESS);
                break;
            }
            //默认
            default:
            {
                if(LangureStu == 1)
                {
                    LangureStr = "No upgrade file";
                }
                else
                {
                    LangureStr = "无升级文件";
                }
				InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_NORMAL);
                break;
            }
            }

            PortStu = PORT_STU_FREE;
        }
        LastPortStu = PORT_STU_FREE;
        return true;
    }
    }

    return false;
}

//机型切换
void MainWindow::on_cbx_model_read_activated(int index)
{
    ModelStu = index;
    ui->cbx_dev->setCheckState(Qt::Unchecked);

    switch(ModelStu){
    case Model_15P:     ModelStu = Model_14P;
    case Model_12:
    case Model_12PM:
    case Model_13:
    case Model_14:
    case Model_15:
    case Model_13P:
    case Model_14P:
    case Model_16:
    case Model_16P:
    {
        //隐藏
        ui->cbx_dev->setVisible(false);
        if(LangureStu == 1)
        {
            LangureStr = "Warm prompt: 12-16 display glass IC has a limit on the number of times to write (generally 1-5 times). After the number of times is written, no data can be written. Please be careful!";
        }
        else
        {
            LangureStr = "温馨提示：12-16的显示玻璃ic有写入次数的限制(一般1-5次)，次数写完后就写不进数据了，请谨慎操作！";
        }
        InsertText(ui->te_prin, LangureStr, NORMAL_FONT_SIZE, APP_FONT_COLOR_WARNNING);
        break;
    }
    default:
    {
        //显示
        ui->cbx_dev->setVisible(false);
        break;
    }
    }
}

void MainWindow::on_cbx_mtsn_write_clicked()
{
    bool CbxStu = ui->cbx_mtsn_write->isChecked();
    ui->btn_mtsn_write->setEnabled(CbxStu);
}
void MainWindow::on_cbx_write_clicked()
{
    bool CbxStu = ui->cbx_write->isChecked();
    ui->btn_write->setEnabled(CbxStu);
}

// 电池修复-读取数据
void MainWindow::on_btn_read_data_bat_clicked()
{
    //停止同步定时器
    SyncTimer->stop();
    Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);
    //提示
    if(Read_Bat_Data_Form_F210())
    {
        Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    }
    else
    {
        Port_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
    }

    SyncTimer->start(TIMER_SYNC_TIME_OUT);
    if(PORT_STU_NULL == PortStu)
    {
        LangureStr_Init_Btn_Unlink(ui->btn_link);
        LangureStr_Init_Btn_Unlink(ui->btn_link_bat);
        on_btn_link_clicked();
    }
}
// 电池修复-写入数据
void MainWindow::on_btn_write_data_bat_clicked()
{
    SyncTimer->stop();
    Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_ALL);

    if(Write_Bat_Data_to_F210())
    {
        Port_Function_Lock_Or_Unlock(DISP_BTN_LOCK_RES);
    }
    else
    {
        Port_Function_Lock_Or_Unlock(DISP_BTN_UNLOCK_ALL);
    }

    SyncTimer->start(TIMER_SYNC_TIME_OUT);
    if(PORT_STU_NULL == PortStu)
    {
        LangureStr_Init_Btn_Unlink(ui->btn_link);
        LangureStr_Init_Btn_Unlink(ui->btn_link_bat);
        on_btn_link_clicked();
    }
}

//锁定解锁状态分支
void MainWindow::Port_Function_Lock_Or_Unlock(int EnabledStu)
{
    switch(EnabledStu) {
    case DISP_BTN_LOCK_ALL:{
        ui->btn_read->setEnabled(false);
        ui->btn_mtsn_read->setEnabled(false);
        ui->btn_write->setEnabled(false);
        ui->btn_mtsn_write->setEnabled(false);
        ui->cbx_write->setEnabled(false);
        ui->cbx_mtsn_write->setEnabled(false);
        ui->btn_upg->setEnabled(false);
        ui->cbx_upg->setEnabled(false);
        AutoUpgFile->setEnabled(false);
		
		//
        ui->btn_read_data_bat->setEnabled(false);
        ui->btn_write_data_bat->setEnabled(false);
        break;
    }
    case DISP_BTN_UNLOCK_ALL:
    case DISP_BTN_LOCK_RES:{
        //
        ui->btn_read->setEnabled(true);
        ui->btn_mtsn_read->setEnabled(true);
        ui->btn_write->setEnabled(false);
        ui->btn_mtsn_write->setEnabled(false);
        ui->cbx_write->setEnabled(true);
        ui->cbx_mtsn_write->setEnabled(true);
        ui->btn_upg->setEnabled(false);
        ui->cbx_upg->setEnabled(true);
        AutoUpgFile->setEnabled(true);
        //
        ui->cbx_upg->setCheckState(Qt::Unchecked);
        ui->cbx_write->setCheckState(Qt::Unchecked);
        ui->cbx_mtsn_write->setCheckState(Qt::Unchecked);
        ui->cbx_dev->setCheckState(Qt::Unchecked);
		//
        ui->btn_read_data_bat->setEnabled(true);
        ui->btn_write_data_bat->setEnabled(true);
        break;
    }
    }
}

void MainWindow::on_sb_BatHeath_valueChanged(int arg1)
{
    uint16_t DesignCap = ui->sb_DesignCAP->value();
    uint16_t FullChargeCap = DesignCap*arg1/100;
    ui->sb_ActualCAP->setValue(FullChargeCap);
}
